package com.example.controller;

import com.example.pojo.Category;
import com.example.result.Result;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public Result getCategory() {
        List<Category> categories = categoryService.getAllCategory();
        return Result.success(200,"操作成功",categories);
    }

    @PostMapping
    public Result addCategory(@RequestBody @Validated Category category) {
        categoryService.addNewCategory(category);
        return Result.success(200,"操作成功");
    }

    @GetMapping("/detail")
    public Result getCategoryById(@RequestParam int id) {
        Category category = categoryService.getCategoryById(id);
        return Result.success(200,"操作成功",category);
    }

    @PutMapping
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.updateCategory(category);
        return Result.success(200,"操作成功");
    }

    @DeleteMapping
    public Result deleteCategoryById(@RequestParam int id) {
        categoryService.deleteCategoryById(id);
        return Result.success(200,"操作成功");
    }
}
